#include "mainscene.h"
#include "ui_mainscene.h"
#include <QPainter>
#include <QDebug>
#include <QTimer>
#include <QTime>
#include "custombutton.h"
#include <QSound>

MainScene::MainScene(QWidget *parent) :
	QMainWindow(parent),
	ui(new Ui::MainScene)
{
	ui->setupUi(this);
	this->setFixedSize(320, 588);
	this->setWindowIcon(QIcon(":/Coin0001.png"));
	this->setWindowTitle("翻金币");
	connect(ui->action1, &QAction::triggered, [=](){
		this->close();
	});

	// 开始按钮音效
	QSound * music_start = new QSound(":/TapButtonSound.wav",this);
	music_start->setLoops(5); //-1为无限循环

	// 开始按钮
	CustomButton * start_button = new CustomButton(":/MenuSceneStartButton.png");
	start_button->setParent(this);
	start_button->move(this->width()*0.5-start_button->width()*0.5,this->height()*0.7);

	// 新建场景
	choose_scene = new ChooseLevelScene;
	// 监听选择关卡返回事件,对第二场景返回事件监听
	connect(choose_scene,&ChooseLevelScene::back_scene,this,[=](){
		// 设置场景在屏幕上的位置
		choose_scene->setGeometry(this->geometry());
		//隐藏此窗口显示下一个界面
		this->choose_scene->hide();
		this->show();
	});

	// 用户点击开始按钮做出反应
	connect(start_button,&CustomButton::clicked,[=](){
		qDebug() << "点击了开始按钮";
		// 播放音效
		music_start->play();
		start_button->zoom1();
		// 等待1秒钟，异步等待，等待期间其他代码会继续执行
		QTimer::singleShot(1000,this,[=](){
			start_button->zoom2();
		});
		QTimer::singleShot(2000,this,[=](){
			// 设置场景在屏幕上的位置
			choose_scene->setGeometry(this->geometry());
			//点击事件完成之后隐藏并显示下一页
			choose_scene->show();
			this->hide();
		});
	});
}

MainScene::~MainScene()
{
	delete ui;
}
void MainScene::paintEvent(QPaintEvent *)
{
	QPainter paint(this);
	QPixmap pix;
	pix.load(":/PlayLevelSceneBg.png");
	paint.drawPixmap(0,0,this->width(),this->height(),pix);

	pix.load(":/Title.png");
	pix = pix.scaled(pix.width()*0.5,pix.height()*0.5);
	paint.drawPixmap(10,30,pix);
}
